package jdbchomework;

import java.sql.*;

public class JdbcUtil {
	static {
		String driverName = "oracle.jdbc.driver.OracleDriver";
		try {
			Class.forName(driverName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConn() {
		String url = "jdbc:oracle:thin:@172.17.1.6:1521" + ":tarena";
		String userName = "hfsd0909";
		String pwd = "tarena";
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, userName, pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	public static void close(ResultSet rs, Statement stmt, Connection con) {
		try {
			if (rs != null)
				rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		try {
			if (stmt != null)
				stmt.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		try {
			if (con != null)
				con.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static Integer toInt(String num) {
		Integer i = null;
		try {
			i = Integer.parseInt(num);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	public static void printRs(ResultSet rs) {
		try {
			ResultSetMetaData meta = rs.getMetaData();
			int cols = meta.getColumnCount();
			StringBuffer sb = new StringBuffer();
			while (rs.next()) {
				for (int i = 1; i <= cols; i++) {
					sb.append(meta.getColumnName(i) + "=");
					sb.append(rs.getString(i) + "  ");
				}
				sb.append("\n");
			}
			System.out.print(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
